<?php

namespace app\admin\controller;

use think\Db;
use think\Cache;

class Blindbox extends Common
{
    public function lists()
    {
        $rejects = Db::name('template_reject_reason')->where('type', 1)->order('sort desc,id asc')->select();
        $this->assign('rejects', json_encode($rejects, JSON_UNESCAPED_UNICODE));

        $where = [];

        $status = input('status/d');
        
        if ($status === null) {
            $status = 0;
        }
        $where['status'] = $status;
        $this->assign('status', $status);
        if ($status == 0) {
            $where['delete_time'] = 0;
        }

        $state = input('state/d');
        
        if ($state !== null) {
            $where['status'] = 1;
            $where['state'] = 0;
            $where['delete_time'] = 0;
            $this->assign('status', 1);
            $this->assign('state', $state);
        }

        $search_typeid = input('param.search_typeid');
        $search_name = input('param.search_name');
        if ($search_name) {
            if (in_array($search_typeid, ['user_id'])) {
                $where[''.$search_typeid] = $search_name;
            }else {
                $where['content'] = ['like', '%' . $search_name . '%'];
            }
        }
        $order = input('order') ? input('order') : 'desc';
        if ($order == 1) {
            $order = 'desc';
        } else if ($order == 2) {
            $order = 'asc';
        }
        
        $data = Db::name('blind_box')
            ->where($where)
            ->order('id ' . $order)
            ->paginate(_getCachePages(), false, ['query' => request()->param()])
            ->each(function($item){
                $item['images'] = $item['images'] ? explode(',', $item['images']) : [];
                $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                $item['audit_time'] = $item['audit_time'] ? date('Y-m-d H:i:s', $item['audit_time']) : '';
                $item['delete_time'] = $item['delete_time'] ? date('Y-m-d H:i:s', $item['delete_time']) : '';
                return $item;
            });
        
        $page = $data->render();
        $this->assign('search_typeid', $search_typeid);
        $this->assign('search_name', $search_name);
        $this->assign('data', $data);
        $this->assign('page', $page);
        
        return $this->fetch();
    }


    public function shenheAll()
    {
        if (request()->isAjax()) {
            $post = input('post.');
            $type = input('type');
            if (input('?post.delAll')) {
                $post = $post['delAll'];
                $db = Db::name('blind_box');
                $timenow = time();
                foreach ($post as $k => $v) {
                    if ($type == 'del') {
                        $res = Db::name('blind_box')->where(['id'=>$v])->find();
                        $dbres = $db->delete($v);
                        if ($dbres) {
                            $images = $res['images'] ? explode(',', $res['images']) : [];
                            foreach ($images as $key => $value) {
                                \app\service\AliOss::delete($value);
                            }
                        }
                    } else {
                        $dy = Db::name('blind_box')->where(['id'=>$v])->find();
                        $update = [
                            'status'    =>  $type,
                            'audit_time'=>$timenow
                        ];
                        $res = $db->where(['id'=>$v])->update($update);
                    }
                }
                if ($res) {
                    $data = ['ret' => '操作成功'];
                } else {
                    $data = ['err' => '操作失败'];
                }
            } else {
                $data = ['err' => '操作失败，未选中数据'];
            }
        } else {
            $data = ['err' => '提交类型错误'];
        }
        return $data;
    }


    public function authadd()
    {
        $id = input('id');

        if (request()->isAjax()) {

            $post = input('post.');

            if ($id) {
                $dy = db('blind_box')->where('id', $id)->find();
                $update = ['status' => $post['shenhe'], 'reason' => $post['reason'], 'audit_time' => time()];
                $db = db('blind_box')->where(['id'=>$id])->update($update);

                if ($db) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            } else {
                $data = ["err" => '参数缺失'];
            }
        }
        return $data;
    }

    public function del() {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $timenow = time();
                $db = Db::name('blind_box')->where(['id' => $id])->update(['delete_time' => $timenow]);
                if ($db) {
                    $data = ['ret' => '操作成功'];
                } else {
                    $data = ['err' => '操作失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }


    public function add() {
        $id = input("id");
        if (request()->isAjax()) {
            $post = input('post.');

            if (!$post['user_id']) {
                return ['err' => '请填写发布用户'];
            }
            $user = Db::name('user')->where(['id'=>$post['user_id'], 'delete_time'=>0, 'reg'=>1])->find();
            if (!$user) return ['err'=>'用户异常'];
            if (!$post['content']) {
                return ['err' => '内容必填'];
            }

            $images = [];
            if ($post['images']) {
                $images = explode(',', $post['images']);
                foreach ($images as $k => $v) {
                    $v = explode('.', $v);
                    $images[$k] = $v[0] . '_thumb.' . $v[1];
                }
                $images = implode(',', $images);
            } else {
                $images = '';
            }
            
            $post['images'] = $images;

            $timenow             = time();
            $post['create_time'] = $timenow;
            $post['audit_time'] = $timenow;
            $post['status']    = 1;
            $post['state']   = 0;
            $db                  = Db::name('blind_box')->insert($post);
            if ($db) {
                $data = ['ret' => '添加成功'];
            } else {
                $data = ['err' => '添加失败'];
            }
        } else {
            $data = $this->fetch();
        }
        return $data;
    }
}